package com.zinnaworks.smart_cloud_gamepad.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.anymote.Key.Code;
import com.zinnaworks.smart_cloud_gamepad.widget.DualJoystickView.JoypadListener;

public class JoystickImageButton extends LinearLayout {

	public static final int INVALID_POINTER_ID = -1;

	private final static int WIDTH_PADDING = 8;
	private final static int HEIGHT_PADDING = 10;
	private final String label;
	private final int imageResId;
	private final Bitmap image;
	String TAG = "JoystickImageButton";

	private View pad;
	private int pointerId = INVALID_POINTER_ID;
	public JoypadListener listener;


	public JoystickImageButton(Context context , int resImage , String label) {
		super(context);
		this.label = label;
		this.imageResId = resImage;
		this.image = BitmapFactory.decodeResource(context.getResources(), imageResId);
		this.measure(this.image.getWidth(), this.image.getHeight());
		setFocusable(true);
		setClickable(true);
		pad = new View(getContext());
	}


	public void setJoypadListener(JoypadListener l)
	{
		this.listener = l;
	}
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect)
	{
		if (gainFocus == true)
		{
			this.setBackgroundColor(Color.rgb(255, 165, 0));
		}
		else
		{
			this.setBackgroundColor(Color.WHITE);
		}
	}

	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
	{
		LayoutParams joyLParams = new LayoutParams(100,getMeasuredHeight());
		
		
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}
	private int measureWidth(int measureSpec)
	{
		int preferred = image.getWidth() * 2;
		return getMeasurement(measureSpec, preferred);
	}

	private int measureHeight(int measureSpec)
	{
		int preferred = image.getHeight() * 2;
		return getMeasurement(measureSpec, preferred);
	}

	private int getMeasurement(int measureSpec, int preferred)
	{
		int specSize = MeasureSpec.getSize(measureSpec);
		int measurement = 0;

		switch(MeasureSpec.getMode(measureSpec))
		{
		case MeasureSpec.EXACTLY:
			// This means the width of this view has been given.
			measurement = specSize;
			break;
		case MeasureSpec.AT_MOST:
			// Take the minimum of the preferred size and what
			// we were told to be.
			measurement = Math.min(preferred, specSize);
			break;
		default:
			measurement = preferred;
			break;
		}

		return measurement;
	}

	

	public String getLabel()
	{
		return label;
	}

	/**
	 * Returns the resource id of the image.
	 */
	public int getImageResId()
	{
		return imageResId;
	}

	

	public void setPointerId(int id) {
		this.pointerId = id;
	}

	public int getPointerId() {
		return pointerId;
	}

	@Override
	public boolean  onTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();
		switch (action & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN: {
				if ( pointerId == INVALID_POINTER_ID ) {
					listener.onJoypadKeyPress(Code.KEYCODE_PAGE_UP);
//					Log.d(TAG, "ACTION_DOWN: " + getPointerId());
					return true;
				}
				break;
			}
		}
		return false;
	}


	@Override
	protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2,
			int paramInt3, int paramInt4) {
		// TODO Auto-generated method stub
		
	}

}

